package com.easylinkin.linkappapi.quality.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.Optional;

import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.quality.dto.QualityActionRecordsDTO;
import com.easylinkin.linkappapi.quality.entity.QualityActionRecords;
import com.easylinkin.linkappapi.quality.mapper.QualityActionRecordsMapper;
import com.easylinkin.linkappapi.quality.service.QualityActionRecordsService;

import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 质量管理查阅记录表 服务实现类
 * </p>
 *
 * @author kan yuanfeng
 * @since 2022-04-13
 */
@Service
public class QualityActionRecordsServiceImpl extends ServiceImpl<QualityActionRecordsMapper, QualityActionRecords> implements QualityActionRecordsService {

  @Autowired
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param qualityActionRecords
   */
  @Override
  public void insert(QualityActionRecords qualityActionRecords) {
    /**
     * 验证重复
     */
//    this.checkExist(qualityActionRecords);
    //设置基本属性
    this.setBase(qualityActionRecords);
    this.save(qualityActionRecords);
  }

  /**
   * @Description: 根据id查询质量管理查阅记录详情
   * @author kan yuanfeng
   * @date 2023/04/04 11:42
   */
  @Override
  public QualityActionRecordsDTO findById(Integer id) {
    RequestModel<QualityActionRecords> requestModel = new RequestModel<>();
    requestModel.setPage(new Page(1,1));
    QualityActionRecords qualityActionRecords = new QualityActionRecords();
    qualityActionRecords.setId(id);
    requestModel.setCustomQueryParams(qualityActionRecords);
    IPage<QualityActionRecordsDTO> page = this.queryListByPage(requestModel);
    if (CollectionUtil.isNotEmpty(page.getRecords())){
      return page.getRecords().get(0);
    }
    return null;
  }

  @Override
  public IPage<QualityActionRecordsDTO> queryListByPage(RequestModel<QualityActionRecords> requestModel) {
    Page page = requestModel.getPage();
    QualityActionRecords qualityActionRecords = requestModel.getCustomQueryParams();
    IPage<QualityActionRecordsDTO> qualityActionRecordsIPage = baseMapper.queryListByPage(page, qualityActionRecords);
    return qualityActionRecordsIPage;
  }

  /**
   * 验证重复
   */
  private void checkExist(QualityActionRecords qualityActionRecords) {
    QueryWrapper<QualityActionRecords> queryWrapper = new QueryWrapper<>();
    //设置判断重复条件
    //编辑的时候存在id
    Optional.ofNullable(qualityActionRecords.getId()).ifPresent(id -> queryWrapper.ne("id_",qualityActionRecords.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该质量管理查阅记录已存在");
    }
  }

  /**
   * 设置基本属性
   * @param qualityActionRecords
   */
  private void setBase(QualityActionRecords qualityActionRecords) {
    qualityActionRecords.setModifyTime(new Date());
    if(null != linkappUserContextProducer.getCurrent()){
      qualityActionRecords.setModifyId(linkappUserContextProducer.getCurrent().getId());
    }
    //没有id就是新增,有就是编辑
    if (null == qualityActionRecords.getId()){
      if(null != linkappUserContextProducer.getCurrent()){
        qualityActionRecords.setCreatorId(linkappUserContextProducer.getCurrent().getId());
      }
      qualityActionRecords.setCreateTime(new Date());
      if(StringUtils.isBlank(qualityActionRecords.getTenantId())){
        qualityActionRecords.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
      }
    }
  }
}
